
//删除功能
 function deleteById(ids) {
     $.ajax({
         url:"/api/student/delete/"+ids,
         method:"DELETE"
     }).done(result=>{
         console.log(result);
         search();
         alert("删除成功")

     }).fail(error=>{
         alert("删除失败")
     })
 }
 function resetByIds(ids) {
     $.ajax({
         url:"/api/student/reset/"+ids,
         method:"GET"
     }).done(result=>{
         console.log(result);
         search();
         alert("重置成功")

     }).fail(error=>{
         alert("重置失败")
     })
 }

 let layerIndex;

 /**
  * 新增或者修改学生信息
  * @param id
  */
 let row;
 function showStudentDlg(data)      {
     let title="新增学生"
     if(data){
         //编辑
         title="编辑学生"
         //读取信息并赋值到表单
         $.each(data,function (key,value){
             var field=$('#studentForm [name="'+key+'"]')
             if(field.is(':radio')){
                 field.filter('[value="'+value+'"]').prop("checked",true) //填充单选框
             }else if(field.is(':checkbox')){
                 field.prop("checked",value==="yes");  //填充复选框
             }else{
                 field.val(value) //填充其他字段
             }
         })


     }else{
         $("#studentForm")[0].reset();
     }
     layerIndex=layer.open({
         type:1,
         area:['500px','auto'],
         title:title,
         content:$("#studentForm")
     })
 }

/**
 * 获取查询条件
 * @returns {{}}
 */
function getSearchCondition(){
     let formData={};
     $('#queryForm').find('input,select').each(function (){
         let name=$(this).attr("name");
         let value=$(this).val();
         if(name&&value){
             formData[name]=value;
         }
     });
     return formData;
 }
 layui.use(function () {
     layui.form.on('submit(stu-dlg)', function (data) {
         event.preventDefault();
         commitStuDlg();
     })
         const table = layui.table;
         let student=getSearchCondition();
         //创建实例渲染
         table.render({
             elem: '#tbStudent',
             url: '/api/student/getbypage',
             method:'POST',
             contentType: 'application/json',
             where:{"data":student},
             page:true,
             cols:[[
                 {type:'checkbox' , fixed:'left'},
                 {field:'id',title:'编号',align:'center',sort:true},
                 {field:'name',title:'姓名',align:'center'},
                 {field:'stunumber',title:'学号',align:'center'},
                 {field:'age',title:'年龄',align:'center',sort:true},
                 {field:'gender',title:'性别',align:'center',sort:true},
                 {field:'password',title:'密码',align:'center'},
                 {field:'operation',title:'操作',width:240,align:'center',templet:'#editTemplate'}
             ]],
             done:function (rs){
                 console.log(rs);
             }
         })
     //表格初始化


     table.on('tool(tbStudent)',function (obj){
         var data=obj.data;
         console.log(data);
         if(obj.event==='edit'){
             row=data.id;
             console.log(row);
             showStudentDlg(data);
         }
         if(obj.event==='del'){
             deleteById(data.id);
         }
})

     /*table.on('sort(tbStudent)',function (obj){
         var field=obj.field;
         var type=obj.type;
         console.log(field);
         console.log(type);
         search();
     })*/
 })

function search(){

     let student=getSearchCondition();
     const table=layui.table;
     table.reloadData('tbStudent',{
         where:{"data":student},
         page:{curr:1}
     })
    console.log(student)
 }
 //清空搜索表中的内容
 function clean(){
     $("#queryForm")[0].reset();
     search();
 }

 function commitStuDlg(){
     let id=row;
     let url;
     let method;
     let formData = $("#studentForm").serialize();
     if(id){
         url="/api/student/update";
         method="PUT";
         formData=formData+"&id="+id
     }else{
         url="/api/student/add";
         method="POST";
     }
     $.ajax({
         url:url,
         method:method,
         data:formData
     }).done(data=>{
         console.log(111);
         console.log(data);
         console.log(data.data.id);
         //刷新页面
         search();
         if(data.data.id){
             console.log("添加成功")
             if(layerIndex){
                 layer.close(layerIndex);
             }
         }

     }).fail((jqXHR, textStatus, errorThrown)=>{
         console.log("添加失败"+textStatus+" "+errorThrown)
         alert("添加失败")
     });
 }

 const ids=[];
function deleteConfirm(){
    layer.confirm('确定删除吗？',{icon:3,title:'提示'},function (index){
        const table=layui.table;
        //获取表格的选中状态
        const checkStatus=table.checkStatus('tbStudent');
        checkStatus.data.forEach(item=>{
            ids.push(item.id);
            console.log(item);
            console.log(item.id);
        })
        console.log(checkStatus);
        console.log(index);
       //如果点击确定
        if(ids.length>0){
            deleteById(ids);
        }
        layer.close(index);
    })
}

function resetPassword(){
    layer.confirm('确定重置密码吗？',{icon:3,title:'提示'},function (index){
        const table=layui.table;
        //获取表格的选中状态
        const checkStatus=table.checkStatus('tbStudent');
        checkStatus.data.forEach(item=>{
            ids.push(item.id);
            console.log(item);
            console.log(item.id);
        })
        console.log(checkStatus);
        console.log(index);
        //如果点击确定
        if(ids.length>0){
            resetByIds(ids);
        }
        layer.close(index);
    })
}
